#新建列表,每三個輸入一個\\hline
#python -u "d:\GitbookLibrary\Discrete-Mathematics-and-Data-Structure\LatexTable.py"
print("請輸入列數")
column = int(input())
ColumnStyle = "|c" * column + "|"
def getCellsbyInput():
    print("請輸入表格內容")
    cells = []
    while True:
        cell = input()
        if cell != "jieshu":
            cells.append(cell)
            continue
        else:
            break
    return cells
def getCellsbyFile(filepath,code):
    cells1 = []
    f1 = open(filepath,"r",encoding = code)
    lines1 = f1.readlines()
    f1.close()
    for line in lines1:
        cells2 = line.strip("\n").split(";;")
        CellNum = 0
        for cell in cells2:
            if cell.count("$") == 1:
                print(cell+"$ error\n")
            cells1.append(cell)
            CellNum += 1
        if CellNum != 3:
            print("CellNum error")
    return cells1
f = open("D:\\GitbookLibrary\\Discrete-Mathematics-and-Data-Structure\\symbol.tex","w",encoding="utf-8")
#cells = getCellsbyInput()
cells = getCellsbyFile("D:\\GitbookLibrary\\Discrete-Mathematics-and-Data-Structure\\symbol.txt","utf-8")
f.write("\\documentclass{article}\n\\usepackage{ctex}\n\\usepackage{supertabular}\n\\usepackage{latexsym}\n\\usepackage{amssymb}\n")
f.write("\\usepackage[landscape]{geometry}\n")#橫置頁面
f.write("\\begin{document}\n\\begin{supertabular}{"+ColumnStyle+"}\n\\hline\n")
num = 0
while num < len(cells):
    if (num+1) % column == 0:
        f.write(cells[num]+"\\\\\n\\hline\n")
        num += 1
        continue
    else:
        f.write(cells[num]+"&")
        num += 1
        continue
f.write("\\end{supertabular}\n\\end{document}")
f.close()
f = open("D:\\GitbookLibrary\\Discrete-Mathematics-and-Data-Structure\\symbol.tex","r",encoding="utf-8")
lines = f.readlines()
f.close()
f = open("D:\\GitbookLibrary\\Discrete-Mathematics-and-Data-Structure\\symbol.tex","w",encoding="utf-8")
num = 0
while num < len(lines) - 2:
    f.write(lines[num])
    if lines[num+2][0] == " " or lines[num+2][0] == "&":
        num += 2
        continue
    else:
        num += 1
        continue
f.write("\\end{supertabular}\n\\end{document}")
f.close()
f = open("D:\\GitbookLibrary\\Discrete-Mathematics-and-Data-Structure\\symbol.tex","r",encoding="utf-8")
lines = f.readlines()
num = 0
while num < len(lines):
    print(str(num+1).zfill(3)+" "+lines[num],end = "")
    num += 1
    continue
f.close()